package network;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerEcho {

	public static void main(String[] args) throws IOException {
		int port = 2501;
		ServerSocket socketS = new ServerSocket(port);
		Socket socketC = null;
		try {
			System.out.println("Server is ready !!");
			socketC = socketS.accept();

			BufferedReader in = new BufferedReader(new InputStreamReader(
					socketC.getInputStream()));
			PrintWriter out = new PrintWriter(new BufferedWriter(
					new OutputStreamWriter(socketC.getOutputStream())), true);

			while (true) {
				String str = in.readLine();
				if (str.equals("END")) {
					break;
				}
				System.out.println("from client >> " + str);
				out.println(str);
			}
		} finally {
			System.out.println("Server is about to shut down");
			socketS.close();
			if (socketC != null) socketC.close();
		}
	}
}
